﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.peddlerUI.peddlerMarketUI.cell {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import flash.text.*;
    import Modules.peddler.*;
    import Display.peddlerUI.peddlerMarketUI.data.*;
    import org.aswing.ext.*;
    import Display.aswingui.*;
    import Display.peddlerUI.peddlerMarketUI.*;
    import Display.peddlerUI.peddlerMarketUI.common.*;

    public class PeddlerCell extends JPanel implements GridListCell {

        protected var m_leftPanel:JPanel;
        protected var m_rightPanel:JPanel;
        protected var m_name_label:JLabel;
        protected var m_info_label:MultilineLabel;
        protected var m_image_panel:ImageLoadPanel;
        protected var m_flag:AssetPane;
        protected var m_data:PeddlerData;
        protected var m_gridList:GridList;
        protected var m_index:int;
        private var m_timer:Timer;

        public function PeddlerCell(_arg1:int, _arg2:LayoutManager=null){
            if (_arg2 == null){
                _arg2 = new BorderLayout();
            };
            super(_arg2);
            this.m_index = _arg1;
            this.init();
        }
        protected function init():void{
            var _local1:Sprite;
            this.m_leftPanel = this.createWestPanel();
            this.m_rightPanel = this.createEastPanel();
            append(this.m_leftPanel, BorderLayout.WEST);
            append(this.m_rightPanel, BorderLayout.EAST);
            if ((this.m_index % 2) == 0){
                _local1 = new Sprite();
                _local1.graphics.beginFill(EmbeddedArt.lightBlueTextColor);
                _local1.graphics.drawRect(0, 0, 595, 50);
                _local1.graphics.endFill();
                ASwingHelper.setBackground(this, _local1);
            };
            ASwingHelper.prepare(this);
        }
        private function createWestPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
            this.m_name_label = ASwingHelper.makeLabel("", EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 12, EmbeddedArt.blackTextColor);
            this.m_image_panel = new ImageLoadPanel();
            _local1.appendAll(this.m_image_panel, this.m_name_label);
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        private function createEastPanel():JPanel{
            var _local1:JPanel = ASwingHelper.makeFlowJPanel(FlowLayout.RIGHT);
            this.m_info_label = new MultilineLabel("");
            this.m_info_label.setEditable(false);
            this.m_info_label.setEnabled(false);
            var _local2:TextFormat = new TextFormat();
            _local2.align = TextFormatAlign.RIGHT;
            this.m_info_label.setTextFormat(_local2);
            this.m_info_label.setPreferredWidth(350);
            this.m_info_label.setFont(new ASFont(EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 12));
            this.m_info_label.setForeground(new ASColor(EmbeddedArt.blackTextColor));
            this.m_flag = new AssetPane();
            _local1.appendAll(this.m_info_label, this.m_flag);
            ASwingHelper.prepare(_local1);
            return (_local1);
        }
        public function setCellValue(_arg1):void{
            if (((this.m_timer) && ((true == this.m_timer.hasEventListener(TimerEvent.TIMER))))){
                this.m_timer.stop();
                this.m_timer.removeEventListener(TimerEvent.TIMER, this.onTimerHandle);
            };
            this.m_data = (_arg1 as PeddlerData);
            if (this.m_data == null){
                return;
            };
            this.m_name_label.setText(this.m_data.peddler_name);
            if (this.m_data.peddler_url != ""){
                this.m_image_panel.setData(this.m_data.peddler_url);
            };
            this.m_info_label.setText(this.m_data.info);
            var _local2:TextFormat = new TextFormat();
            _local2.align = TextFormatAlign.RIGHT;
            this.m_info_label.setTextFormat(_local2);
            var _local3:DisplayObject;
            if (this.m_flag.hasEventListener(MouseEvent.CLICK)){
                this.m_flag.removeEventListener(MouseEvent.CLICK, this.askDonuts);
            };
            if (this.m_data.hasFood){
                _local3 = new EmbeddedArt.hasDonuts();
            } else {
                _local3 = new EmbeddedArt.noDonuts();
                this.m_flag.addEventListener(MouseEvent.CLICK, this.askDonuts);
            };
            this.m_flag.setAsset(_local3);
            if (!this.m_data.hasFood){
                this.m_flag.buttonMode = true;
            } else {
                this.m_flag.buttonMode = false;
            };
            if (this.m_data.status == PeddlerModel.STATE_REST){
                if (this.m_data.restSleepTimeSecond != -1){
                    if (this.m_timer == null){
                        this.m_timer = new Timer(1000);
                    };
                    this.m_timer.addEventListener(TimerEvent.TIMER, this.onTimerHandle);
                    this.m_timer.start();
                };
            };
            ASwingHelper.prepare(this.m_info_label);
            ASwingHelper.prepare(this);
        }
        private function onTimerHandle(_arg1:TimerEvent):void{
            this.m_data.restSleepTimeSecond = (this.m_data.restSleepTimeSecond - 1);
            if (this.m_data.restSleepTimeSecond <= 0){
                this.m_timer.removeEventListener(TimerEvent.TIMER, this.onTimerHandle);
                this.m_timer.stop();
                this.m_data.status = HarvestableResource.STATE_PLOWED;
                this.setCellValue(this.m_data);
            };
            var _local2:String = GameUtil.formatMinutesSeconds(this.m_data.restSleepTimeSecond);
            var _local3:String = (_local2 + this.m_data.info);
            this.m_info_label.setText(_local3);
            var _local4:TextFormat = new TextFormat();
            _local4.align = TextFormatAlign.RIGHT;
            if (this.m_data.status == PeddlerModel.STATE_REST){
                this.m_info_label.setTextFormat(_local4);
            };
        }
        public function getCellValue(){
            return (this.m_data);
        }
        public function getCellComponent():Component{
            return (this);
        }
        public function setGridListCellStatus(_arg1:GridList, _arg2:Boolean, _arg3:int):void{
            if (_arg1){
                this.m_gridList = _arg1;
                this.m_index = _arg3;
                _arg1.setTileWidth(595);
                _arg1.setTileHeight(65);
            };
        }
        private function askDonuts(_arg1:MouseEvent):void{
            PeddlersPanel.askDonuts();
        }

    }
}//package Display.peddlerUI.peddlerMarketUI.cell 
